// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require("vscode");
const vuedoc = require("@vuedoc/md");
const marked = require("marked");
function showDoc(content) {
  // Create and show a new webview
  const panel = vscode.window.createWebviewPanel(
    "vueDocPreview", // Identifies the type of the webview. Used internally
    "vue doc preview", // Title of the panel displayed to the user
    vscode.ViewColumn.One, // Editor column to show the new webview panel in.
    {
      enableScripts: true,
    } // Webview options. More on these later.
  );
  panel.webview.html = `
  <!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Marked in the browser</title>
  <link rel="stylesheet" href="https://cdn.bootcss.com/github-markdown-css/3.0.1/github-markdown.css">
  <style>
  #content{
    background:white;
    padding:10px 20px;
    max-width:1000px;
  }
  </style>
</head>
<body>
  <div id="content" class="markdown-body">${content}</div>

</body>
</html>
`;
}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {
  // Use the console to output diagnostic information (console.log) and errors (console.error)
  // This line of code will only be executed once when your extension is activated
  console.log('Congratulations, your extension "vue-doc-md" is now active!');
  let workspace = vscode.workspace;
  let fs = workspace.fs;

  // The command has been defined in the package.json file
  // Now provide the implementation of the command with  registerCommand
  // The commandId parameter must match the command field in package.json
  let disposable = vscode.commands.registerCommand(
    "vue-doc-md.makeDoc",
    async function (selectUrl) {
      let types = vscode.FileType;
      let isDir = types.Directory == (await fs.stat(selectUrl)).type;
      
      let filenames;
      if (isDir) {
        const pattern1 = new vscode.RelativePattern(selectUrl, "**/*.vue");
        filenames = (
          await workspace.findFiles(pattern1, "**​/node_modules/**", 0)
        ).map((item) => {
          return item.fsPath;
        });
      } else {
        filenames = [selectUrl.fsPath];
      }

      // Display a message box to the user
      let options = {
        join: false,
        filenames: filenames,
      };
      vuedoc
        // @ts-ignore
        .md(options)
        .then((document) => {
          if (Array.isArray(document)) {
            document = document.join("\r\r");
          }
          console.log(document);
          showDoc(marked(document));
        })
        .catch((err) => console.error(err));
    }
  );

  context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
function deactivate() {}

module.exports = {
  activate,
  deactivate,
};
